package com.fuershiye.woman.monitor.service.impl;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.fuershiye.woman.monitor.model.IndicatorType;
import com.fuershiye.woman.monitor.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fuershiye.woman.monitor.model.Department;
import com.fuershiye.woman.monitor.model.Indicator;
import com.fuershiye.woman.monitor.utils.MyUtilString;

@Service("bridgeService")
public class BridgeServiceImpl implements BridgeService {

	@Autowired
	UserService userService;
	@Autowired
	IndicatorTypeService indicatorTypeService;

	@Override
	public Indicator modifyIndicator(Indicator indicator, HttpServletRequest request) {

		String name = request.getParameter("name");
		String level = request.getParameter("level");
		String code = request.getParameter("code");
		String monitor = request.getParameter("monitor");
		String type = request.getParameter("type");
		String pid = request.getParameter("pid");
		String prule = request.getParameter("prule");
		String sort = request.getParameter("sort");
		String unit = request.getParameter("unit");
		String places = request.getParameter("places");
//		String base = request.getParameter("base");
		String aim = request.getParameter("aim");
		String rule = request.getParameter("rule");
		String country = request.getParameter("country");
		String province = request.getParameter("province");
		String sex = request.getParameter("sex");
		String need = request.getParameter("need");
		String hide = request.getParameter("hide");
		String description = request.getParameter("description");
		String supplement = request.getParameter("supplement");

		if (!MyUtilString.isBlankNullZero(name)) {
			indicator.setName(name);
		}
		if (!MyUtilString.isBlankNull(level)) {
			indicator.setLevel(Integer.valueOf(level));
		}
		if (!MyUtilString.isBlankNullZero(code)) {
			indicator.setCode(code);
		}
		if (!MyUtilString.isBlankNullZero(type)) {
			indicator.setType(Long.valueOf(type));
			IndicatorType indicatorType = indicatorTypeService.get(Long.valueOf(type));
			if(indicatorType!=null){
				indicator.setFormsid(indicatorType.getFormsid());
			}
		}
		if (!MyUtilString.isBlankNull(pid)) {
			indicator.setPid(Long.valueOf(pid));
		}else{
			indicator.setPid(null);
		}
		if (!MyUtilString.isBlankNull(prule)) {
			indicator.setPrule(Integer.valueOf(prule));
		}else{
			indicator.setPrule(0);
		}
		if (!MyUtilString.isBlankNullZero(monitor)) {
			indicator.setMonitor(Integer.parseInt(monitor));
		}
		if (!MyUtilString.isBlankNullZero(sort)) {
			indicator.setSort(Long.valueOf(sort));
		}
		if (!MyUtilString.isBlankNullZero(unit)) {
			indicator.setUnit(unit);
		}
		if (!MyUtilString.isBlankNull(places)) {
			indicator.setPlaces(Integer.valueOf(places));
		}
		if (!MyUtilString.isBlankNull(rule)) {
			indicator.setRule(Integer.valueOf(rule));
		}
		if (!MyUtilString.isBlankNull(aim)) {
			indicator.setAim(Double.valueOf(aim));
		}
//		if (!MyUtilString.isBlankNull(base)) {
//			indicator.setBase(Double.valueOf(base));
//		}
		if (!MyUtilString.isBlankNull(country) &&
				!Integer.valueOf(country).equals(indicator.getCountry())) {
			indicator.setCountry(Integer.valueOf(country));
		}
		if (!MyUtilString.isBlankNull(province)&&
				!Integer.valueOf(province).equals(indicator.getProvince())) {
			indicator.setProvince(Integer.valueOf(province));
		}
		if (!MyUtilString.isBlankNull(need)) {
			indicator.setNeed(Integer.valueOf(need));
		}
		if (!MyUtilString.isBlankNull(hide)) {
			indicator.setHide(Integer.valueOf(hide));
		}
		if (!MyUtilString.isBlankNull(sex)) {
			indicator.setSex(Integer.valueOf(sex));
		}
		indicator.setDescription(description);
		indicator.setSupplement(supplement);
		indicator.setMtime(new Date());
		indicator.setStatus(1);

		return indicator;
	}
	
	@Override
	public Department getUserDepartment() {
		String uname = SecurityUtils.getSubject().getPrincipal() + "";
		return userService.getDepartIdByName(uname);
	}
	
}
